package view.log
{
	import business.AdminUserDelegate;
	import business.LogDelegate;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import model.AppCons;
	
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.ComboBox;
	import mx.controls.DataGrid;
	import mx.controls.DateField;
	import mx.controls.LinkButton;
	import mx.controls.TextInput;
	import mx.rpc.events.FaultEvent;

	[Bindable]
	public class LogClass extends VBox
	{
		//view
		public var users:ComboBox;
		public var modules:ComboBox;
		public var startTime:DateField;
		public var endTime:DateField;
		public var targetId:TextInput;
		public var targetType:ComboBox;
		public var logGrid:DataGrid;
		
		public var page:int;
		public var pageIndices:Array;
		public var pagecnt:int;
		public var userDatas:Array;
		public var modulesDatas:Array;
		public var cardTypes:Array;
		public var allCount:int;
		
		private var _m_items:ArrayCollection;
		private var adminUserDelegate:AdminUserDelegate;
		private var logDelegate:LogDelegate;
		
		public function LogClass()
		{
			super();
			adminUserDelegate = new AdminUserDelegate(this);
			logDelegate = new LogDelegate(this);
			this.addEventListener("dataReflesh",makePageIndices);
		}
		
		public function init():void
		{
			getAllUsers();
			getAllModules();
			getCardTypes();
		}
		
		public function clearTime(event:Event):void
		{
			startTime.selectedDate = null;
			endTime.selectedDate = null;
		}
		
		public function search(event:Event, page1:int=-1):void
		{
			var searchObj:Object = new Object();
			var issetOne:Boolean = false;
			
			if(users.selectedLabel && users.selectedItem.id != -1)
			{
				searchObj.uid = users.selectedItem.id;
				issetOne = true;
			}
			else if(!users.selectedLabel)
			{
				//searchObj.username = users.text;
				var uid:String = getUidFromName(users.text);
				if(uid && uid != '-1')
				{
					issetOne = true;
					searchObj.uid = getUidFromName(users.text);
				}
				else if(!uid)
				{
					Alert.show('该用户不存在', '提示');
					return ;
				}
			}
			
			if( modules.selectedItem.index != -1 )
			{
				searchObj.mid = modules.selectedItem.index;
				issetOne = true;
			}
			
			if( targetType.selectedItem.index != -1 )
			{
				searchObj.targetType = targetType.selectedItem.index;
				issetOne = true;
			}
			
			if( startTime.selectedDate )
			{
				searchObj.startTime = startTime.selectedDate.getTime() / 1000;
				issetOne = true;
			}
			
			if( endTime.selectedDate )
			{
				searchObj.endTime = endTime.selectedDate.getTime() / 1000 + 86400;
				issetOne = true;
			}
			
			if( targetId.text.length > 0 )
			{
				searchObj.targetId = targetId.text;
				issetOne = true;
			}
			
			var pageT:int;
			if(page1 != -1)
			{
				pageT = page1;
			}
			else
			{
				pageT = page;
			}
			if(issetOne)
			{
				logDelegate.search(searchObj, pageT);
			}
			else
			{
				logDelegate.getLogs(pageT);
			}
		}
		
		private function getUidFromName(name:String):String
		{
			for each(var item:Object in userDatas)
			{
				if(name == item.name)
				{
					return item.id;
				}
			}
			return null;
		}
		
		public function getResult(result:Object, token:Object = null):void
		{
			result = result.result;
			
			if(result.code == 0)
			{
				this.page = result.page>0?result.page:0;
				this.pagecnt = result.pagecnt>1?result.pagecnt:1;				
				var backData:Array = result.data as Array;
				for each(var data:Object in backData)
				{
					formatData(data);
				}
				this.m_items = new ArrayCollection(backData);	
				allCount = result.len;			
			}
			else
			{
				Alert.show(result.msg);					
			}			
		}
		
		private function formatData(obj:Object):void
		{
			var time:Date = new Date();
			time.setTime(Number(obj.time)*1000);
			obj.time = time.getFullYear() + '-' + int(time.getMonth()+1) + '-' + time.getDate() + ' ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();
			
			for each(var item:Object in AppCons.MODULES)
			{
				if(obj.mid == item.index)
				{
					obj.module = item.label;
					break ;
				}
			}
			
			if(obj.targetType == '1')
			{
				obj.targetType = '连环画';
			}
			else if(obj.targetType == '2')
			{
				obj.targetType = '录像';
			}
			
		}		
		
		public function goto(evt:MouseEvent):void
		{
			var page1:int = LinkButton(evt.currentTarget).data.page;
			// then  go to database get data;
			search(null,page1);
		}
		
		public function gotoPage(page:int):void
		{
			search(null,page-1);
		}
		
		public function get m_items():ArrayCollection
		{
			return this._m_items;
		}		
		
		public function set m_items(value:ArrayCollection):void
		{
			this._m_items = value;
			dispatchEvent(new Event('dataReflesh'));
		}
		
		public function getAllUsers():void
		{
			adminUserDelegate.getAllUsers();
		}
		
		public function getAllModules():void
		{
			modulesDatas = AppCons.MODULES;
			modulesDatas.reverse();
			modulesDatas.push({index:-1,label:'全部'});
			modulesDatas.reverse();
		}
		
		public function getCardTypes():void
		{
			this.cardTypes = [{index:-1,label:'全部'},{index:2,label:'录像'},{index:1,label:'连环画'}];
		}
		
		public function getAllUsersResult(result:Object, token:Object = null):void
		{
			result = result.result;
			if(result.code == 0)
			{
				userDatas = result.data as Array;
				userDatas.reverse();
				userDatas.push({id:-1,name:'全部'});
				userDatas.reverse();
			}
			else
			{
				Alert.show(result.msg);
			}		
		}
		
		public function fault(error:FaultEvent,token:Object = null):void
		{
			Alert.show(error.fault.faultString,'error');
		}
		
		public function onDataGridItemClick(event:Event):void
		{
			
		}
		
		public function setDate(num:int):void
		{
			
			if((!startTime.selectedDate && !endTime.selectedDate) || num == 0)
			{
				startTime.selectedDate = new Date();
				startTime.selectedDate.setDate(startTime.selectedDate.getDate() + num);
				
				endTime.selectedDate = new Date();
				endTime.selectedDate.setDate(endTime.selectedDate.getDate() + num);	
				
				return ;					
			}
			
			if(startTime.selectedDate && endTime.selectedDate && (endTime.selectedDate.getDate() == startTime.selectedDate.getDate()))
			{
				
				var currentDate:Date = startTime.selectedDate;
				currentDate.setDate(currentDate.getDate() + num);
				startTime.selectedDate = currentDate;
				endTime.selectedDate = currentDate;
								
				return ;
			}
			
		}	
		
		private function makePageIndices(event:Event=null):void
		{
			var a:Array = new Array();
			a.push({page:0,desc:'首页'});			
			a.push({page:page-1,desc:'前一页'});			
			a.push({page:page+1,desc:'后一页'});
			a.push({page:pagecnt-1,desc:'尾页'});
			this.pageIndices = a;
		}												
		
	}
}